# -*- coding: utf-8 -*-
from flask import Blueprint,render_template,request
from common.models.user import User
from common.libs.Helper import ops_renderJSON,ops_renderErrJSON,ops_render
from common.models.admin_function import AdminFunction
from common.models.user import User
import json, requests

index_page = Blueprint( "index_page",__name__ )

@index_page.route("/index")
def login():
    return ops_render( "/member/login.html")


@index_page.route("/index2")
def index2():
    return ops_render( "/common/layout2.html")

# @index_page.route("/")
# def login1():
#     return ops_render( "/member/login1.html")

@index_page.route("/dd")
def ddlogin():

    return ops_render( "/member/a.html")


@index_page.route("/ddd")
def dddlogin():

    authCode = request.args.get('code')
    print("code为",authCode)

    appID = 'dingt4jsgirf9m6iy1gu'
    appSecret = 'czAGSBIZJIvQX4f6YGEFr0vfLNE80Er6sEH-CzbWsAs-0yJitDixGvd_cWTYZlVZ'  # 开发者关于微信小程序的appSecret

    token_params = {
        'appkey': appID,
        'appsecret': appSecret,
    }
    dd_token = 'https://oapi.dingtalk.com/gettoken'
    token_response = requests.get(dd_token, params=token_params)  # 向API发起GET请求
    tokenData = token_response.json()
    access_token = tokenData['access_token']

    userid_params = {
        "code": authCode,
    }
    dd_userid = 'https://oapi.dingtalk.com/topapi/v2/user/getuserinfo?access_token=' + access_token
    userid_response = requests.post(dd_userid, params=userid_params)  # 向API发起GET请求
    useridData = userid_response.json()
    print(useridData)
    result = useridData['result']
    name = result['name']
    id = result['userid']

    return json.dumps(
        {"code": 200, "msg": "登录成功", "name": name, "userid": id},
        indent=4,
        sort_keys=True,
        default=str,
        ensure_ascii=False)

@index_page.route("/ht", methods=['GET', 'POST'])
def index():
    uid = request.args.get('uid')
    userinfo = User.query.filter(User.id==uid).first()
    list=userinfo.role.split(',')
    a=AdminFunction.query.filter(AdminFunction.role.in_(list)).all()


    return ops_render( "index.html",{"adminfunction":a})